package com.game.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.game.pojo.po.Email;
import com.game.pojo.po.IntruderData;
import com.game.pojo.po.IntruderLog;
import com.game.pojo.vo.IntruderLogVo;
import com.game.pojo.vo.Result;
import com.game.service.IntruderDataService;
import com.game.service.IntruderLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lvhao
 * @since 2020-09-17
 */
@CrossOrigin
@RestController
@RequestMapping("/api")
@Api(tags = "入侵日志")
@Transactional
public class IntruderLogController {
    @Autowired
    private IntruderLogService intruderLogService;
    @Autowired
    private IntruderDataService intruderDataService;

    @GetMapping("/intruder_log/user/{id}")
    @ApiOperation("根据用户id查询")
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 202, message = "查询失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result getById(@PathVariable("id") int id){
        try{
            List<IntruderLogVo> intruderLogVoList = new ArrayList<>();
            IntruderLogVo intruderLogVo = new IntruderLogVo();
            // 获取日志
            QueryWrapper<IntruderLog> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", id);
            if(intruderLogService.list(queryWrapper).isEmpty()){
                return new Result(202,"查询失败","记录不存在");
            }
            List<IntruderLog> intruderLogList = intruderLogService.list(queryWrapper);
            for (IntruderLog intruderLog : intruderLogList){
                BeanUtils.copyProperties(intruderLog, intruderLogVo);
                // 获取数据列表
                QueryWrapper<IntruderData> queryWrapper1 = new QueryWrapper<>();
                queryWrapper.eq("intruder_log_id", intruderLog.getId());
                intruderLogVo.setIntruderDataList(intruderDataService.list(queryWrapper1));
            }
            intruderLogVoList.add(intruderLogVo);
            if(intruderLogVoList.isEmpty()){
                return new Result(202,"查询失败","记录不存在");
            }else{
                return new Result(200,"查询成功",intruderLogVoList);
            }
        }catch (Exception e){
            return new Result(400,"参数有误",e.getLocalizedMessage());
        }
    }

    @PostMapping("/intruder_log")
    @ApiOperation("新建")
    @ApiResponses({
            @ApiResponse(code = 200, message = "新建成功"),
            @ApiResponse(code = 202, message = "新建失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result add(IntruderLogVo intruderLogVo){
        try{
            if(intruderLogVo.getTime()!=null && !intruderLogVo.getTime().equals("")){
                intruderLogVo.setTime(LocalDateTime.parse(intruderLogVo.getTime(), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString());
            }else{
                intruderLogVo.setTime(LocalDateTime.now().toString());
            }
            // 插入日志
            IntruderLog intruderLog = new IntruderLog();
            BeanUtils.copyProperties(intruderLogVo, intruderLog);
            if(!intruderLogService.save(intruderLog)){
                return new Result(202,"新建失败",null);
            }
            // 插入日志的数据列表
            System.out.println(intruderLog.toString());
            System.out.println(intruderLogVo.getIntruderDataList().toString());
            for(IntruderData intruderData : intruderLogVo.getIntruderDataList()){
                intruderData.setIntruderLogId(intruderLog.getId());
                if(!intruderDataService.save(intruderData)){
                    return new Result(202,"新建失败",null);
                }
            }
            return new Result(200,"新建成功",intruderLogVo);
        }catch (Exception e){
            return new Result(400,"参数有误",e.getLocalizedMessage());
        }
    }

    @DeleteMapping("/intruder_log/{id}")
    @ApiOperation("根据id删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 202, message = "删除失败"),
            @ApiResponse(code = 400, message = "参数有误")
    })
    public Result deleteById(@PathVariable int id){
        try{
            // 删除日志
            if(!intruderLogService.removeById(id)){
                return new Result(202,"删除失败",id);
            }
            //删除日志的数据列表
            QueryWrapper<IntruderData> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("intruder_log_id", id);
            if(!intruderDataService.remove(queryWrapper)){
                return new Result(202,"删除失败",id);
            }
            return new Result(200,"删除成功",id);

        }catch (Exception e){
            return new Result(400,"参数有误","请求参数错误");
        }
    }
}

